package com.mq.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mq.pojo.Admin;
import com.mq.service.IAdminService;
import com.mq.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author maoqi
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private IAdminService adminService;

    @GetMapping("/register")
    public R register(Admin admin) {
        System.out.println("admin = "+ admin);
        R result = adminService.addAdmin(admin);
        return result;
    }

//    @GetMapping("/add")
//    public R add(Admin admin) {
//        System.out.println("admin = "+ admin);
//        boolean flag = adminService.save(admin);
//        if(flag){
//            return  R.ok("注册成功!");
//        }else{
//            return  R.fail("注册失败!");
//        }
//    }

    @GetMapping("/login")
    public R login(String username, String password) {
        //检查是否获取成功
        System.out.println("username= " + username + " password= " + password);
        //调用业务逻辑层的方法
        R result = adminService.adminLogin(username, password);
        return result;
    }

    @GetMapping("/list")
    public R list(int page,int limit) {
        //page页码   limit每页显示的记录数
        System.out.println("页码: " + page + " 每页显示" + limit + "条记录");
        //配置了Mybatis-plus提供的分页插件
        Page<Admin> tool = new Page<>(page,limit);//创建分页插件工具
        //调用IService接口提供的page方法
        Page<Admin> adminPage = adminService.page(tool);
        //将结果传递给前端页面
        long totalSize = adminPage.getTotal();   //总记录个数
        List<Admin> list = adminPage.getRecords();  //当前页面中显示的记录
        return R.ok("数据查询成功!").setCount(totalSize).setData(list);
    }

    @PostMapping("/updateStatus")
    public R updateStatus(@RequestBody Admin admin) {
        System.out.println("admin = "+ admin);
        boolean flag =adminService.updateById(admin);
        return flag?R.ok("状态更新成功"):R.fail("状态更新失败");
    }

    @GetMapping("/delete")
    public R delete(int id) {
        System.out.println("id = " + id);
        boolean flag = adminService.removeById(id);
        return flag?R.ok("删除成功!"):R.fail("删除失败!");
    }



}

